import React from 'react'
import { DeviceEventEmitter } from 'react-native'

import { connect } from 'react-redux'

import BaseHomePage from 'src/pageP/home/home-page/index'
import { homePostList } from 'src/utils/RequestAPI'
import JobCell from 'src/components/job-cell/index'
import CustomButton from 'src/components/button/CustomButton'
import { C_POSITION_REFRESH } from 'src/pageC/tabbar'
import RefreshState from 'src/components/custom-flat-list/RefreshState'
import { IS_ANDROID } from 'src/utils/ScreenUtil'

const mapActiontoProps = dispatch => ({})

@connect(
  ({ landmark, userinfo }) => ({
    landmark,
    userId: userinfo.userId
  }),
  mapActiontoProps
)
class HomePage extends BaseHomePage {
  constructor (props) {
    super(props)
    this.pageType = 'C'
    this.pageKey = 'page'
    this.cityNameKey = 'city'
    this.categoryKey = 'category'
    this.areaKey = 'area'
    this.orderKey = 'order'
    this.homeListParams = this._getHomeListParams()
    this.orderLabel = [
      {
        isAll: true,
        label: '综合排序',
        type: this.orderKey,
        value: 'order'
      },
      {
        isAll: false,
        label: '最近发布',
        type: this.orderKey,
        value: 'order'
      },
      {
        isAll: false,
        label: '离我最近',
        type: this.orderKey,
        value: 'distance'
      }
    ]
  }

  _getHomeListParams = () => {
    const { landmark: { currentLocation } } = this.props
    const latitude = currentLocation && currentLocation.lat
    const longitude = currentLocation && currentLocation.lng
    const homeListParams = [
      {
        [this.pageKey]: 1,
        type: 0
      },
      {
        [this.pageKey]: 1,
        practiceLabel: 1
      },
      {
        [this.pageKey]: 1,
        type: 1
      }
    ]
    if (latitude && longitude) {
      homeListParams.forEach((item) => {
        item.latitude = latitude
        item.longitude = longitude
      })
    }
    return homeListParams
  }

  postFunc () {
    return homePostList
  }

  /**
   * 每一条的样式
   * @param item
   * @param index
   * @returns {XML}
   * @private
   */
  renderItem ({ item, index }) {
    const { landmark } = this.props
    return (
      <CustomButton onPress={this._jobClick} params={{ item, index }}>
        <JobCell item={item} landmark={landmark} onlyShowFast={true}/>
      </CustomButton>
    )
  }

  _jobClick = ({ item, index }) => {
    const blockCallback = () => {
      console.log('blockCallback', index)
      const { dataArr } = this.state
      this.setState({
        dataArr: dataArr.filter(e => e.id !== item.id)
      })
    }
    this.props.navigation.navigate('CJobdetail', {
      id: item.id,
      cityName: this.props.landmark.cityName,
      blockCallback
    })
  }

  componentDidMount () {
    super.componentDidMount()
    this.tabRefreshSubscription = DeviceEventEmitter.addListener(
      C_POSITION_REFRESH,
      this.tabRefreshClick
    )
  }

  componentWillUnmount () {
    super.componentWillUnmount()
    this.tabRefreshSubscription && this.tabRefreshSubscription.remove()
    console.log('C---->HomePage--->componentWillUnmount')
  }

  tabRefreshClick = () => {
    console.log('C---->HomePage--->tabRefreshClick')
    this._scrollToOffset(0, false)
    this.scrollableTabRef.beginHeaderRefresh(RefreshState.Idle)
    this._homeFamousList()
  }
}

export default HomePage

// https://github.com/Micjoyce/ScrollViewWithTabView
